package com.starbaba.stat.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.stat.model.LoanBackCore;
import com.starbaba.stat.model.StatActivityChannelModel;
import com.starbaba.stat.utils.Constant;

@SuppressWarnings("rawtypes")
@Component
public class BackCoreStatService {

	static BaseDao dao = new BaseDao(Constant.LOAN_STAT_DB_POOL);  //正式stat
	private BaseDao daoLoan = new BaseDao("tt1");  ///测试数据库loan
	
	public void showPage(Map<String, Object> paramsMap) throws Exception {  
		List<LoanBackCore> channelList = daoLoan.list("SELECT c.channel_code channelId,c.channel_name channel from comm_product_channel c", LoanBackCore.class);
		List<LoanBackCore> prdList = daoLoan.list("SELECT p.prdid appProdId,p.name prodName from comm_product_prdid p", LoanBackCore.class);
		StatActivityChannelModel all = new StatActivityChannelModel();
//		all.setPrd_id("全部");
//		all.setChannel_name("全部");
//		prdList.add(0, all);
//		channelList.add(0, all);
		paramsMap.put("prdList", prdList);
		paramsMap.put("channelList", channelList);
	}
	
	public List getList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception {
		
		String startTime = (String) paramsMap.get("startTime");
		String endTime = (String) paramsMap.get("endTime");
		String platform = (String) paramsMap.get("platform");
		String prd_id = (String) paramsMap.get("prd_id");
		String channel = (String) paramsMap.get("channel"); 
		String showway = (String) paramsMap.get("showway"); 
		
		StringBuilder whereParams = new StringBuilder();
		if (StringUtils.isNotEmpty(startTime)) {
			whereParams.append("and c.date_star >= '" + startTime + "' ");
		}
		if (StringUtils.isNotEmpty(endTime)) {
			whereParams.append("and c.date_star <= '" + endTime + "' ");
		}
		if (StringUtils.isNotEmpty(platform)) {
			whereParams.append("and c.platform = '" + platform + "' ");
		}
		if (StringUtils.isNotEmpty(prd_id)) {
			whereParams.append("and c.app_prd_id = '" + prd_id + "' ");
		}
		if (StringUtils.isNotEmpty(channel) && !"全部".equals(channel)) {
			whereParams.append("and c.channel = '" + channel + "' ");
		}
		if (StringUtils.isNotEmpty(showway) && !"0".equals(showway)) {
			whereParams.append("and c.show_way = '" + showway + "' ");
		}
		
		
		StringBuilder listSql = new StringBuilder("SELECT c.app_prd_id appProdId,c.channel channel,c.check_in_uv checkInUv,"
				+ "c.credit_details_page_uv creditDetailsPageUv,c.date_end dateEnd,c.date_star dateStart,c.day_activity_pv dayActivityPv,"
				+ "c.day_activity_uv dayActivityUv,c.loan_details_page_pv loanDetailsPagePv,c.loan_details_page_uv loanDetailsPageUv, "
				+ "c.lucky_turn_uv luckyTurnUv,c.new_activity_accounts newActivityAccounts,c.new_info_uv newInfoUv,c.new_phone_uv newPhoneUv,"
				+ "c.participate_btn_pv participateBtnPv,c.participate_btn_uv participateBtnUv,c.platform platform,c.show_way showWay,"
				+ "c.next_day_rate nextDayRate,c.three_day_rate threeDayRate "
				+ "from stat_loan_back_core c where 1=1 ");
		listSql.append(whereParams);
		listSql.append("order by c.date_star desc ");
		
		String sqlCount = "select count(1) from (" + listSql.toString() + ") z ";
		if (start != null && length != null) {
			listSql.append("limit " + start + ", " + length);
		}
		int count = dao.getInt(sqlCount);
		
		String prodSql = "SELECT p.prdid appProdId,p.name prodName from comm_product_prdid p";
		List<LoanBackCore> prodList = daoLoan.list(prodSql,LoanBackCore.class);
		
		String channelSql = "SELECT c.channel_code channelId,c.channel_name channel from comm_product_channel c";
		List<LoanBackCore> channelList = daoLoan.list(channelSql,LoanBackCore.class);
		
		List<LoanBackCore> list = dao.list(listSql.toString(), LoanBackCore.class);
		for (LoanBackCore model : list) {
			if (model.getPlatform() == null || "null".equals(model.getPlatform())) {
				model.setPlatform("未知");
			}
			String prodId = model.getAppProdId();   
			if ("-20".equals(prodId)) {
				model.setProdName("全部");
			}else if(prodId != null && !"null".equals(prodId)){
				String prodName = getProdName(prodList,prodId);
				model.setProdName(prodName);
			}else{
				model.setProdName("未知");
			}
			String channelId = model.getChannel();  
			if ("-20".equals(channelId)) {
				model.setChannel("全部");
			}else if(channelId != null && !"null".equals(channelId)){
				String channelName = getChannelName(channelList,channelId);
				model.setChannel(channelName);
			}else{
				model.setChannel("未知");
			}
		}
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		return list;
	}
	
	public String getChannelName(List<LoanBackCore> channelList,String id){
		for(LoanBackCore core:channelList){
			if(core.getChannelId().equals(id)){
				return core.getChannel();
			}
		}
		return id;
	}
	
	public String getProdName(List<LoanBackCore> prodList,String id){
		for(LoanBackCore core:prodList){
			if(core.getAppProdId().equals(id)){
				return core.getProdName();
			}
		}
		return id;
	}
	
	public static String getPercent(Long a, Long b) {
		return (float) Math.round((float) a / b * 10000) / 100 + "%";
	}
	
	public static String getPercent(String a, String b) {
		if (StringUtils.isEmpty(a) || StringUtils.isEmpty(b)){
			return "0";
		}
		Long al = Long.parseLong(a);
		Long bl = Long.parseLong(b);

		return (float) Math.round((float) al / bl * 10000) / 100 + "%";
	}
	
}
